package soul.resource;

import soul.domain.code.Status;
import soul.domain.response.AuthParam;
import soul.domain.response.VerificationCode;
import soul.domain.response.Response;
import soul.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import soul.service.UserService;

import java.util.*;

/**
 * Created by sunzhengjie on 2016/11/21.
 */
@Controller
@RequestMapping("/")
public class AuthResource {

    private static final List<Map.Entry<String, String>> CODE_LIST = new ArrayList<>();

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    @ResponseBody
    public Response login(@RequestBody AuthParam param) {

        if (StringUtils.isEmpty(param.getUser().getName())) {
            return Response.ok(Status.fail, "用户名不能为空");
        }
        if (StringUtils.isEmpty(param.getUser().getPassword())) {
            return Response.ok(Status.fail, "密码不能为空");
        }

        if(param.needVerifyCode()){
            if (StringUtils.isEmpty(param.getCode().getToken())) {
                return Response.ok(Status.fail, "system-error:token");
            }
            if (StringUtils.isEmpty(param.getCode().getCode())) {
                return Response.ok(Status.fail, "验证码不能为空");
            }
            if (!checkVerificationCode(param.getCode())) {
                return Response.ok(Status.fail,"验证码不正确");
            }
        }

        User userGot = userService.checkUserCredential(param.getUser());
        if(userGot == null){
            return Response.ok(Status.fail,"用户名密码不正确");
        }

        return Response.ok(Status.Ok);
    }

    @RequestMapping("/verification_code")
    @ResponseBody
    public Response getCode() {
        Map.Entry<String, String> tokenAndCode = tokenAndCode();
        return Response.ok(Status.Ok, new VerificationCode(tokenAndCode.getKey(), tokenAndCode.getValue()));
    }

    private static boolean checkVerificationCode(VerificationCode code) {
        return code.getCode().equalsIgnoreCase(CODE_CACHE.get(code.getToken()));
    }

    private static Map.Entry<String, String> tokenAndCode() {
        Random random = new Random();
        int index = random.nextInt(CODE_LIST.size());
        return CODE_LIST.get(index);
    }

    private static final Map<String, String> CODE_CACHE = new HashMap<>();

    static {
        CODE_CACHE.put(UUID.randomUUID().toString(), "AS12");
        CODE_CACHE.put(UUID.randomUUID().toString(), "XC12");
        CODE_CACHE.put(UUID.randomUUID().toString(), "AD12");
        CODE_CACHE.put(UUID.randomUUID().toString(), "J1DF");
        CODE_CACHE.put(UUID.randomUUID().toString(), "DG13");
        CODE_CACHE.put(UUID.randomUUID().toString(), "HF12");
        CODE_CACHE.put(UUID.randomUUID().toString(), "GH90");
        CODE_CACHE.put(UUID.randomUUID().toString(), "LMN8");
        CODE_CACHE.put(UUID.randomUUID().toString(), "FS79");
        CODE_CACHE.put(UUID.randomUUID().toString(), "GGT7");
        CODE_CACHE.put(UUID.randomUUID().toString(), "FHN8");
        CODE_CACHE.put(UUID.randomUUID().toString(), "J78K");
        CODE_CACHE.put(UUID.randomUUID().toString(), "GSR4");
        CODE_CACHE.put(UUID.randomUUID().toString(), "FH8L");
        CODE_CACHE.put(UUID.randomUUID().toString(), "JG8K");
        CODE_CACHE.put(UUID.randomUUID().toString(), "DAKL");
        CODE_CACHE.put(UUID.randomUUID().toString(), "GU44");
        CODE_CACHE.put(UUID.randomUUID().toString(), "GD13");
        CODE_CACHE.put(UUID.randomUUID().toString(), "DAD1");
        CODE_CACHE.put(UUID.randomUUID().toString(), "D12D");
    }

    static {
        CODE_CACHE.entrySet().forEach(entry -> CODE_LIST.add(entry));
    }
}
